package com.doupi.partner.service.impl;

import com.doupi.partner.dal.UserRelationMapper;
import com.doupi.partner.model.UserRelation;
import com.doupi.partner.service.UserRelationService;
import com.doupi.partner.util.Pagination;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@org.springframework.stereotype.Service
public class UserRelationServiceImpl implements UserRelationService {
	private final UserRelationMapper userRelationMapper;

	@Autowired
	public UserRelationServiceImpl(UserRelationMapper userRelationMapper) {
		this.userRelationMapper = userRelationMapper;
	}

	public int deleteByPrimaryKey(Long id) {
		return userRelationMapper.deleteByPrimaryKey(id);
	}

	public int insert(UserRelation record) {
		return userRelationMapper.insert(record);
	}

	public UserRelation selectByPrimaryKey(Long id) {
		return userRelationMapper.selectByPrimaryKey(id);
	}

	public int updateByPrimaryKeySelective(UserRelation record) {
		return userRelationMapper.updateByPrimaryKeySelective(record);
	}

	public int countUserRelation(UserRelation userRelation) {
		return userRelationMapper.countUserRelation(userRelation);
	}

	public List<UserRelation> queryUserRelations(UserRelation userRelation, RowBounds rowBounds) {
		return userRelationMapper.queryUserRelations(userRelation, rowBounds);
	}

	public List<UserRelation> selectUserRelationForUpdate(UserRelation userRelation) {
		return userRelationMapper.selectUserRelationForUpdate(userRelation);
	}

	public UserRelation selectUserRelationForUpdateByPrimaryKey(Long id) {
		return userRelationMapper.selectUserRelationForUpdateByPrimaryKey(id);
	}

	public Pagination<UserRelation, UserRelation> queryUserRelations(Pagination<UserRelation, UserRelation> pagination) {
		int count = countUserRelation(pagination.getParam());
		pagination.setTotalCount(count);
		if (count > 0) {
			pagination.setRecords(queryUserRelations(pagination.getParam(), new RowBounds(pagination.getOffset(), pagination.getLimit())));
		}
		return pagination;
	}

	@Override
	public int saveUserRelation(UserRelation record) {
		int ret;
		if (null == record.getId()) {
			ret = insert(record);
		} else {
			ret = updateByPrimaryKeySelective(record);
		}
		return ret;
	}

	@Override
	public Pagination<UserRelation, UserRelation> queryUserRelationsWithAnnotation(Pagination<UserRelation, UserRelation> pagination) {
		int count = userRelationMapper.countUserRelationWithAnnotation(pagination.getParam());
		pagination.setTotalCount(count);
		if (count > 0) {
			pagination.setRecords(userRelationMapper.queryUserRelationWithAnnotation(pagination.getParam(), new RowBounds(pagination.getOffset(), pagination.getLimit())));
		}
		return pagination;
	}

}
